package cn.com.thinvent.dispatch.service.biz.impl;

import cn.com.thinvent.dispatch.dao.ActivityManagementMapper;
import cn.com.thinvent.dispatch.entity.ActivityManagement;
import cn.com.thinvent.dispatch.entity.response.PagedGridResult;
import cn.com.thinvent.dispatch.service.biz.ActivityManagementService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Component
public class ActivityManagementServiceImpl implements ActivityManagementService {

    @Resource
    private ActivityManagementMapper activityManagementMapper;

    @Override
    public void addActivity(ActivityManagement activityManagement) {
        activityManagementMapper.insertActivity(activityManagement);
    }

    @Override
    public void updateActivity(ActivityManagement activityManagement) {
        activityManagementMapper.updateActivity(activityManagement);
    }

    @Override
    public void deleteActivityById(String id) {
        activityManagementMapper.deleteActivityById(id);
    }

    @Override
    public ActivityManagement getActivityById(String id) {
        return activityManagementMapper.selectActivityById(id);
    }

    @Override
    public PagedGridResult<ActivityManagement> getActivityList(int pageNum, int pageSize, String activityType, Date startTime, Date endTime) {
        // 开始分页
        PageHelper.startPage(pageNum, pageSize);

        // 查询数据
        List<ActivityManagement> activities = activityManagementMapper.selectByCriteria(activityType, startTime, endTime);

        // 查询总条数
        int totalRecords = activityManagementMapper.countByCriteria(activityType, startTime, endTime);

        // 计算总页数
        int totalPages = (int) Math.ceil((double) totalRecords / pageSize);

        // 构建并返回 PagedGridResult
        PagedGridResult<ActivityManagement> result = new PagedGridResult<>();
        result.setPage(pageNum);
        result.setTotal(totalPages);
        result.setRecords(totalRecords);
        result.setRows(activities);

        return result;
    }
}
